const fp = require("lodash/fp")

const cars = [
    {
        name: "Ferrari FF",
        horsepower: 660,
        dollar_value: 700000,
        in_stock: true,
    },
    {
        name: "Spyker C12 Zagato",
        horsepower: 650,
        dollar_value: 748000,
        in_stock: false,
    },
    {
        name: "Jaguar XKR-S",
        horsepower: 550,
        dollar_value: 13200,
        in_stock: false,
    },
    {
        name: "Audi R8",
        horsepower: 525,
        dollar_value: 114200,
        in_stock: false,
    },
    {
        name: "Aston Martin One-77",
        horsepower: 700,
        dollar_value: 1300000,
        in_stock: false,
    },
    {
        name: "Pagani Huayra",
        horsepower: 700,
        dollar_value: 1300000,
        in_stock: false,
    },

]

// 2.1 使用 fp.flowRight() 重新实现下面这个函数
let isLastInStock = function (cars) {
    // 获取最后一条数据
    let last_car = fp.last(cars)
    // 获取最后一条数据的 in_stock 属性值
    return fp.prop("in_stock", last_car)
}

const f = fp.flowRight(fp.prop("in_stock"), fp.last)
console.log(f(cars));
console.log("===2.1 end===");
// 2.2  使用fp.flowRight() fp.prop(),fp.first() 获取第一个car的name
const f1 = fp.flowRight(fp.prop("name"), fp.first)
console.log(f1(cars));
console.log("===2.2 end===");
// 2.3 使用帮助函数 _average 重构 averageDollarValue , 使用函数组合的方式实现
let _average = function (xs) {
    return fp.reduce(fp.add, 0, xs) / xs.length
}
let averageDollarValue = function (cars) {
    let dollar_values = fp.map(function (car) {
        return car.dollar_value
    }, cars)
    return _average(dollar_values)
}
// console.log(averageDollarValue(cars));

// let f3 = fp.flowRight(fp.flowRight(fp.map()))
// console.log(f3(cars));

let f3 = fp.flowRight(_average,fp.map("dollar_value"));
console.log(f3(cars));
console.log("===2.3 end===");




//2.4 使用flowRight 写一个 sanitizeNames() 函数,
//  返回一个下划线链接的小写字符串,把数组中的name转换为这种形式
//  例如 写一个sanitizeNames(['Hello World'])=> ["hello_world"]
//  let _underscore = fp.replace(/\W+/g,'-')

let _underscore = fp.replace(/\W+/g,'_')

// let f4= fp.flowRight(_underscore,fp.map(fp.toLower), fp.split(' '))

// console.log(f4(['Hello World']));


let  sanitizeNames = fp.flowRight(
    fp.map(_underscore),
    fp.map(fp.toLower),
)
console.log(sanitizeNames(["Hello World"]));

